package maosy.oauth2.client.test.http

import com.google.gson.Gson
import maosy.lib.http.HttpUtils
import org.apache.commons.codec.digest.DigestUtils
import org.apache.http.Header
import org.apache.http.HeaderElement
import org.apache.http.NameValuePair
import org.apache.http.client.methods.HttpPost
import org.apache.http.message.BasicHeader
import org.apache.http.message.BasicNameValuePair
import org.apache.http.message.HeaderValueFormatter
import org.junit.Test
import java.util.*

class ClientTest {

    private val baseUrl = "http://127.0.0.1:8055"
    private val testUrl = "http://192.168.2.104:7355"

    private val gson = Gson()

    @Test
    fun testUrl() {
        val result = HttpUtils.httpPostBody(
            "$baseUrl/auth/api?access_token=1a3ad863-e396-4947-8d76-1c2804feaa18",
            gson.toJson(mapOf("test" to 1, "test2" to 2))
        )
        println(result)
    }

    @Test
    fun clientToken() {
        val params = mutableListOf<NameValuePair>(
            BasicNameValuePair("username", "user_1"),
            BasicNameValuePair("password", "123456"),
            BasicNameValuePair("grant_type", "client_credentials"),
            BasicNameValuePair("scope", "select"),
            BasicNameValuePair("client_id", "client_1"),
            BasicNameValuePair("client_secret", "123456")
        )

        val headers = mutableListOf<Header>(
            BasicHeader("Content-Type", "multipart/form-data")
        )
        val result = HttpUtils.httpPost(
            "$baseUrl/oauth/token",
            params, headers
        )
        val sb = StringBuilder("")
        params.forEach {
            sb.append(it.name).append("=").append(it.value).append("&")
        }
        sb.deleteCharAt(sb.length - 1)
        println("url:$baseUrl/oauth/token?$sb")

        println(result)
    }

    @Test
    fun userToken2() {
        val params = mutableListOf<NameValuePair>(
            BasicNameValuePair("grant_type", "password"),
//            BasicNameValuePair("scope", "server"),
            BasicNameValuePair("client_id", "demoApp"),
            BasicNameValuePair("client_secret", "demoAppSecret"),
            BasicNameValuePair("username", "demoUser2"),
            BasicNameValuePair("password", "123456")
        )

        val result = HttpUtils.httpPost(
            "$baseUrl/oauth/token",
            params
        )
        val sb = StringBuilder("")
        params.forEach {
            sb.append(it.name).append("=").append(it.value).append("&")
        }
        sb.deleteCharAt(sb.length - 1)
        println("url:$baseUrl/oauth/token?$sb")

        println(result)
    }

    @Test
    fun userToken() {
        val params = mutableListOf<NameValuePair>(
            BasicNameValuePair("grant_type", "password"),
            BasicNameValuePair("client_id", "JCSS-SERVER"),
            BasicNameValuePair("client_secret", "123456"),
            BasicNameValuePair("username", "admin"),
            BasicNameValuePair("password", DigestUtils.md5Hex("123456"))
        )

        val result = HttpUtils.httpPost(
            "$baseUrl/oauth/token",
            params
        )
        val sb = StringBuilder("")
        params.forEach {
            sb.append(it.name).append("=").append(it.value).append("&")
        }
        sb.deleteCharAt(sb.length - 1)
        println("url:$baseUrl/oauth/token?$sb")
        println(result)
    }


    @Test
    fun token() {
        val params = mutableListOf<NameValuePair>(
            BasicNameValuePair("grant_type", "client_credentials"),
            BasicNameValuePair("scope", "all"),
            BasicNameValuePair("client_id", "JCSS-SERVER"),
            BasicNameValuePair("client_secret", "123456")
        )
        val result = HttpUtils.httpPost(
            "$baseUrl/oauth/token",
            params
        )
        val sb = StringBuilder("")
        params.forEach {
            sb.append(it.name).append("=").append(it.value).append("&")
        }
        sb.deleteCharAt(sb.length - 1)
        println("url:$baseUrl/oauth/token?$sb")

        println(result)
    }

}